﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IankerProj.Models;
using PagedList.Mvc;
using PagedList;
using System.IO;

namespace IankerProj.Controllers
{
    public class HomeController : Controller
    {
        
        AnkerDBEntities db = new AnkerDBEntities();
        //
        // GET: /Home/

        public ActionResult TrangChu(int? page)
        {

            int pageSize = 5;
            int pageNumber = (page ?? 1);
            var p = db.Thietbis.ToList();
            return View(p.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult Footer()
        {
            return PartialView();
        }
        public ActionResult Support()
        {
            var c = db.Danhmucs.Where(p => p.idcha!=0).ToList();
            return View(c);
        }
        public ActionResult thietbidetail(int term)
        {
            var p = db.Thietbis.Where(c => c.idcha == term);
            return View(p);
        }
        public ActionResult Chitiet(int term)
        {
            var p = db.Thietbis.Where(c => c.id == term);
            return View(p);
        }
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Logint()
        {
            string email=Request.Form["username"].ToString();
            string matkhau=Request.Form["password"].ToString();
            if (ModelState.IsValid)
            {
                var p = db.Taikhoans.Where(c => c.email == email).FirstOrDefault();
                if (p != null)
                {
                    if (p.matkhau == matkhau)
                    {
                        Session["email"] = p.email;
                        Session["ten"] = p.ten;
                        return RedirectToAction("Trangchu", "Home");
                    }
                    else { ModelState.AddModelError("", "Mật khẩu không chính xác"); }

                }
                else { ModelState.AddModelError("", "Tên Tài khoản không chính xác"); }
            }
          return View("Login");
        }
        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Registert()
        {
            Taikhoan t = new Taikhoan();

            t.email = Request.Form["email"].ToString();
            t.ten = Request.Form["username"].ToString();
            t.matkhau = Request.Form["password"].ToString();
            t.gioitinh = true;
           
            
            t.ngaysinh =Convert.ToDateTime(Request.Form["ngaysinh"]);
            t.diachi = Request.Form["country"].ToString();
            db.Taikhoans.Add(t);
            db.SaveChanges();
            return RedirectToAction("Login","Home");
        }
        public ActionResult Logout()
        {
            Session["email"] = null;
            Session["ten"] = null;
            return RedirectToAction("Trangchu","Home");
        }
    }
}
